// the main file for QueryProcessor
// 1. call QueryPrepocessor to pass the query;
// 2. call QueryEvaluator to compute the result;
// 3. call QueryProjector to organize the result;

#include "QueryPreprocessor.h"
#include "QueryEvaluator.h"
#include "QueryProjector.h"
#include "QueryProcessor.h"

using namespace std;

string QueryProcessor::query;
TNode QueryProcessor::root;
bool QueryProcessor::resultBoolean;
string QueryProcessor::resultType;
vector<int> QueryProcessor::resultIndex;

QueryProcessor::QueryProcessor(){ // initializer - to initialize all private attributes
    query = "";
    // initialize all the rest...
}

//------ setter method for query preprocessor------
bool QueryProcessor::setQuery(string q){ // passing query
	query = q;
    // call QueryPreprocessor::parse()
	TNode n = QueryPreprocessor::parse(query);
	if(n.getType()==NONE){// for invalid query, e.g. syntax error or synonym in query not appears in definition table
		return false;
	}
	else{
		root = n;// set query tree by pass this root node to QueryEvaluator
		return true;
	}
}
//------ getter method for query preprocessor------
string QueryProcessor::getQueryStr(){
    return query;
}

TNode QueryProcessor::getRootOfQueryTree(){
    return root;
}

//------ setter method for query evaluator -------

//------ getter method for query evaluator -------
void QueryProcessor::getResult(){
    QueryEvaluator::setQueryTree(root);
    QueryEvaluator::evaluate();
	resultIndex = QueryEvaluator::getResult();
	resultBoolean = QueryEvaluator::getResultBoolean();
	resultType = QueryEvaluator::getResultType();
}
//------ setter method for query projector -------
//------ getter method for query projector -------
void QueryProcessor::organizeResult(list<string>& results){
	QueryProjector::arrangeResult(resultBoolean,resultIndex,resultType);
    QueryProjector::getResult(results);
}

